<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="原料名称" prop="rawMaterialName">
      <el-input v-model="dataForm.rawMaterialName" placeholder="原料名称"></el-input>
    </el-form-item>
    <el-form-item label="原料类型" prop="rawMaterialTypeId">
		<el-select v-model="dataForm.rawMaterialTypeId" placeholder="请选择">
		    <el-option
		      v-for="item in rawTypeList"
		      :key="item.id"
		      :label="item.typeName"
		      :value="item.id">
		    </el-option>
		  </el-select>
    </el-form-item>
    <el-form-item label="规格" prop="specs">
      <el-input v-model="dataForm.specs" placeholder="规格"></el-input>
    </el-form-item>
    <el-form-item label="产地" prop="placeOfRigin">
      <el-input v-model="dataForm.placeOfRigin" placeholder="产地"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :disabled="disabledSubmit">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
				disabledSubmit:false,
		rawTypeList:[],
        visible: false,
        dataForm: {
          id: 0,
          sellerId: '',
          rawMaterialName: '',
          rawMaterialTypeId: '',
          specs: '',
          placeOfRigin: '',
          createTime: '',
          remark: ''
        },
        dataRule: {
          sellerId: [
            { required: true, message: '租户不能为空', trigger: 'blur' }
          ],
          rawMaterialName: [
            { required: true, message: '原料名称不能为空', trigger: 'blur' }
          ],
          rawMaterialTypeId: [
            { required: true, message: '原料类型不能为空', trigger: 'blur' }
          ],

        }
      }
    },
    methods: {
      init (id) {

		//类型
		this.$http({
		  url: this.$http.adornUrl('/factory/tfactoryrawmeterialtype/list'),
		  method: 'get',
		}).then(({data}) => {
		  if (data && data.code === 0) {
		    this.rawTypeList = data.page.list
		  } else {
		    this.rawTypeList = []
		  }
		})

        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/factory/tfactoryrawmeterial/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.sellerId = data.tFactoryRawMeterial.sellerId
                this.dataForm.rawMaterialName = data.tFactoryRawMeterial.rawMaterialName
                this.dataForm.rawMaterialTypeId = data.tFactoryRawMeterial.rawMaterialTypeId
                this.dataForm.specs = data.tFactoryRawMeterial.specs
                this.dataForm.placeOfRigin = data.tFactoryRawMeterial.placeOfRigin
                this.dataForm.createTime = data.tFactoryRawMeterial.createTime
                this.dataForm.remark = data.tFactoryRawMeterial.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
				this.disabledSubmit = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/factory/tfactoryrawmeterial/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'sellerId': this.dataForm.sellerId,
                'rawMaterialName': this.dataForm.rawMaterialName,
                'rawMaterialTypeId': this.dataForm.rawMaterialTypeId,
                'specs': this.dataForm.specs,
                'placeOfRigin': this.dataForm.placeOfRigin,
                'createTime': this.dataForm.createTime,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
										this.disabledSubmit = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          } else {
            this.disabledSubmit = false
          }
        })
      }
    }
  }
</script>
